using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Vue_AspNetCore_Project.Models;
using Microsoft.AspNetCore.Identity;

namespace Vue_AspNetCore_Project.Data
{
    public static class DbContextExtensions
    {
        public static UserManager<ApplicationUser> UserManager { get; set; }
        public static RoleManager<ApplicationRole> RoleManager { get; set; }
        public static async Task EnsureSeeded(this ApplicationDbContext context)
        {
            

            if (await RoleManager.RoleExistsAsync("SuperAdmin") == false)
            {
                await RoleManager.CreateAsync(new ApplicationRole("SuperAdmin"));
            }

            if (await RoleManager.RoleExistsAsync("CustomerAdmin") == false)
            {
                await RoleManager.CreateAsync(new ApplicationRole("CustomerAdmin"));
            }

            if (await RoleManager.RoleExistsAsync("Customer") == false)
            {
                await RoleManager.CreateAsync(new ApplicationRole("Customer"));
            }
            if (await UserManager.FindByEmailAsync("13700000000") == null)
            {
                var user = new ApplicationUser
                {

                    UserName = "13700000000",
                    Email = "mi.tian2008@aliyun.com",
                    EmailConfirmed = true,
                    LockoutEnabled = false
                };

                await UserManager.CreateAsync(user, "Admin@123456");
            }

            var admin = await UserManager.FindByEmailAsync("mi.tian2008@aliyun.com");

            if (await UserManager.IsInRoleAsync(admin, "SuperAdmin") == false)
            {
                await UserManager.AddToRoleAsync(admin, "SuperAdmin");
            }

 
        }
        private static void AddUserAndRoles(ApplicationDbContext context)
        {
            if (RoleManager.RoleExistsAsync("SuperAdmin").GetAwaiter().GetResult() == false)
            {
                RoleManager.CreateAsync(new ApplicationRole("SuperAdmin")).GetAwaiter().GetResult();
            }

            if (RoleManager.RoleExistsAsync("CustomerAdmin").GetAwaiter().GetResult() == false)
            {
                RoleManager.CreateAsync(new ApplicationRole("CustomerAdmin")).GetAwaiter().GetResult();
            }

            if (RoleManager.RoleExistsAsync("Customer").GetAwaiter().GetResult() == false)
            {
                RoleManager.CreateAsync(new ApplicationRole("Customer")).GetAwaiter().GetResult();
            }
            if (UserManager.FindByEmailAsync("13700000000").GetAwaiter().GetResult() == null)
            {
                var user = new ApplicationUser
                {

                    UserName = "13700000000",
                    Email = "mi.tian2008@aliyun.com",
                    EmailConfirmed = true,
                    LockoutEnabled = false
                };

                UserManager.CreateAsync(user, "Admin@123456").GetAwaiter().GetResult();
            }

            var admin = UserManager.FindByEmailAsync("mi.tian2008@aliyun.com").GetAwaiter().GetResult();

            if (UserManager.IsInRoleAsync(admin, "SuperAdmin").GetAwaiter().GetResult() == false)
            {
                UserManager.AddToRoleAsync(admin, "SuperAdmin").GetAwaiter().GetResult(); ;
            }
        }


    }
}